home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
10,000 Great Games
/
10,000 Great Games.iso
/
Product
/
66
/
data1.cab
/
Source_Files
/
Src
/
LoadSave.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2000-01-16
|
5KB
|
241 lines
#include "stdafx.h"
static CFile *save_file;
static int paste_game_y_start, paste_back_y_start, copy_paste_mode = FALSE;
int load_y_offset(cSurface *surface)
{
if (!copy_paste_mode)
return 0;
if (surface == game_surface)
return game_surface->start - paste_game_y_start;
if (surface == back_surface)
return back_surface->start - paste_back_y_start;
return 0;
}
static void load_cutfile(cParse *list)
{
paste_game_y_start = list->get_int("GAMESTART");
paste_back_y_start = list->get_int("BACKSTART");
}
static void load_object(char *type, cParse *list)
{
cProperties *p = cProperties::find_w_error(type, list->get_string("NAME"));
int x = list->get_int("X"),
y = list->get_int("Y") + load_y_offset(*p->objtype->surface);
cGameObject *g = cObjectTypes::make(x, y, p);
g->load(list);
if (copy_paste_mode)
g->create_editables(TRUE);
}
void load_level(CFile *f, int paste)
{
// Set mode
copy_paste_mode = paste;
// Load loadable parts
cParse *lvl = 0, *type = 0, *object = 0;
cParse::parse_file(f, &lvl);
// Process parts
for (;;)
{
if (lvl == 0 || lvl->cmp("TYPE"))
{
// First process old object
if (type != 0)
{
if (eq(type->value, "BLASTER LEVEL"))
level->make(object);
else if (eq(type->value, "CUT FILE"))
load_cutfile(object);
else
load_object(type->value, object);
object->delete_list();
type->delete_list();
}
// Store new type or end loop
if (lvl != 0)
lvl->relink((cList **)&type);
else
break;
}
else
lvl->relink_end((cList **)&object);
}
// End of copy/paste mode
copy_paste_mode = FALSE;
// Create editables
cEditable::make_editables();
// Sort objects
update_onscreen_list();
}
void load_level(char *name, int paste)
{
TRY
{
CFile f(name, CFile::modeRead | CFile::shareDenyWrite);
load_level(&f, paste);
}
CATCH(CFileException, e)
{
error("Unable to load level %s", name);
}
END_CATCH
}
static void save_cutfile()
{
save_level_string("TYPE", "CUT FILE");
save_level_int("GAMESTART", game_surface->start);
save_level_int("BACKSTART", back_surface->start);
}
static void save_object(cGameObject *o)
{
o->save();
save_file->Write("\n", 1);
}
static void save_all(cGameObject *list)
{
for (cGameObject *o = list; o != 0; o = (cGameObject *)o->next)
if (!copy_paste_mode || (o->editable != 0 && o->editable->selected))
save_object(o);
}
void save_level(CFile *f, int copy)
{
// Set mode
copy_paste_mode = copy;
// Store file pointer
save_file = f;
// Save level object
if (!copy_paste_mode)
level->save();
else
save_cutfile();
save_file->Write("\n", 1);
// Save other objects in objtypes list
for (int i = 0; objtypes[i].type != 0; i++)
if (!objtypes[i].list_occured_earlier)
{
save_all(*objtypes[i].belowscreen);
save_all(*objtypes[i].onscreen);
save_all(*objtypes[i].abovescreen);
}
// End of copy/paste mode
copy_paste_mode = FALSE;
}
void save_level(char *name, int copy)
{
TRY
{
CFile f(name, CFile::modeCreate | CFile::modeWrite | CFile::shareExclusive);
save_level(&f, copy);
}
CATCH(CFileException, e)
{
error("Unable to save level %s", name);
}
END_CATCH
}
void save_level_string(char *label, char *value)
{
char *s = construct("%s=%s;", label, value);
save_file->Write(s, strlen(s));
}
void save_level_int(char *label, int value)
{
char *s = construct("%s=%d;", label, value);
save_file->Write(s, strlen(s));
}
void save_level_fix(char *label, fix value)
{
char *s = construct("%s=%lg;", label, (double)value);
save_file->Write(s, strlen(s));
}
void save_level_spot(char *label, cSpot *spot)
{
char *s = construct("%s=%d,%d;", label, spot->x, spot->y);
save_file->Write(s, strlen(s));
}
void kill_level()
{
// Delete editables
cEditable::delete_editables();
// Delete standard game objects
for (int i = 0; objtypes[i].type != 0; i++)
{
(*objtypes[i].belowscreen)->delete_list();
(*objtypes[i].onscreen)->delete_list();
(*objtypes[i].abovescreen)->delete_list();
}
// Delete some other objects
scores->delete_list();
bubbles->delete_list();
players->delete_list();
disaster->delete_list();
texts->delete_list();
images_left->delete_list();
images_right->delete_list();
texts_left->delete_list();
texts_right->delete_list();
// And last but not least (can be generated by deleting other objects)
parts->delete_list();
effects->delete_list();
}